package com.ssm.common.tool;

import java.util.Calendar;

public class EncryptAndDecryptFile {

	private String key = "gesoft-kem";

	public EncryptAndDecryptFile() {
		Calendar cal = Calendar.getInstance();
		key += cal.get(Calendar.YEAR) + "" + cal.get(Calendar.MONTH) + "" + cal.get(Calendar.DAY_OF_MONTH);
	}

	public String Encrypt(Object content) {
		String encrypt = "";
		try {
			encrypt = setParameter(content.toString(), key);
		} catch (Exception e) {
			encrypt = content.toString();
		}
		return encrypt;
	}

	public String Dncrypt(Object content) {
		String dncrypt = "";
		try {
			dncrypt = getParameter(content.toString(), key);
		} catch (Exception e) {
			dncrypt = content.toString();
		}
		return dncrypt;
	}

	private static String setParameter(String content, String key) throws Exception {
		if ("".equals(content))
			return "";
		else
			return encryptContent(content, key).toUpperCase();
	}

	private static String encryptContent(String content, String desKey) throws Exception {
		byte[] byt = content.getBytes();
		return byte2hex(lockChar(byt, byt.length, desKey));
	}

	private static byte[] lockChar(byte[] byText, int iTextLen, String strPass) throws Exception {
		int nLenght = strPass.length();
		for (int j = 0; j < iTextLen; j++) {
			for (int i = 0; i < nLenght; i++) {
				int iByText = byText[j];
				int iPass = strPass.charAt(i);
				iByText = iByText ^ iPass;
				byText[j] = (byte) iByText;
			}
		}
		return byText;
	}

	protected static String byte2hex(byte[] b) throws Exception {
		StringBuffer hs = new StringBuffer();
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = Integer.toHexString(b[n] & 0XFF);
			if (stmp.length() == 1)
				hs.append("0").append(stmp);
			else
				hs.append(stmp);
		}
		return hs.toString();
	}

	private static String getParameter(String content, String key) throws Exception {
		if ("".equals(content))
			return "";
		else
			return decryptKey(content.toLowerCase(), key);
	}

	private static String decryptKey(String content, String key) throws Exception {
		return decryptContent(hex2byte(content), key.getBytes());
	}

	protected static byte[] hex2byte(String str) throws Exception {
		int length = str.length() / 2;
		byte bt[] = new byte[length];
		for (int n = 0; n < length; n++) {
			bt[n] = (byte) Integer.parseInt(str.substring(2 * n, 2 * (n + 1)), 16);
		}
		return bt;
	}

	private static String decryptContent(byte[] content, byte[] desKey) throws Exception {
		byte[] bytDes = unLockChar(content, content.length, new String(desKey));
		return new String(bytDes, "GBK");
	}

	private static byte[] unLockChar(byte[] byText, int iTextLen, String strPass) throws Exception {
		int nLenght = strPass.length();
		for (int j = 0; j < iTextLen; j++) {
			for (int i = 0; i < nLenght; i++) {
				int iByText = byText[j];
				int iPass = strPass.charAt(i);
				iByText = iByText ^ iPass;
				byText[j] = (byte) iByText;
			}
		}
		return byText;
	}

	public static void main(String[] args) {
		EncryptAndDecryptFile ed = new EncryptAndDecryptFile();
		String ret = ed.Encrypt("1/1/1_");
		System.out.println(ret);
	}
	
}